#include "cyclic_buffer.h"
#include "string.h"


void incr_head (CBUFFER *pcb);
void incr_tail (CBUFFER *pcb);

void cb_write (CBUFFER *pcb, const void *pd)
{
	void *ptmp = pcb->pcbuffer + pcb->head;
	memcpy (ptmp, pd, pcb->unit_size);
	incr_head (pcb);
	return;
}

void *cb_read (CBUFFER *pcb)
{
	void *pres;
	if (pcb->head == pcb->tail)
		// buffer leer
		return 0;
	pres = pcb->pcbuffer+pcb->tail;
	incr_tail (pcb);
	return pres;
}

void incr_head (CBUFFER *pcb)
{
	pcb->head += pcb->unit_size;
	if (pcb->head >= pcb->size)
		pcb->head = 0;
	if (pcb->head == pcb->tail)
		//buffer over
		pcb->tail += pcb->unit_size;
	if (pcb->tail >= pcb->size)
		pcb->tail = 0;
}

void incr_tail (CBUFFER *pcb)
{
	if (pcb->tail == pcb->head)
		//buffer leer
		return;

	pcb->tail += pcb->unit_size;
	if (pcb->tail >= pcb->size)
		pcb->tail = 0;
}

//size_t getSize (CBUFFER *pcb)
//{
//	char sz = pcb->head - pcb->tail;
//	sz = sz < 0?pcb->size - sz:sz;
//	return sz+1;
//}
